package com.kg.module.jtnc.applet.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kg.core.annotation.NoRepeatSubmit;
import com.kg.core.exception.BaseException;
import com.kg.module.jtnc.applet.dto.RwdfDTO;
import com.kg.module.jtnc.applet.dto.RwwcDTO;
import com.kg.module.jtnc.jy.dto.KrJyDTO;
import com.kg.module.jtnc.jy.dto.convert.KrJyConvert;
import com.kg.module.jtnc.jy.entity.KrJy;
import com.kg.module.jtnc.jy.service.KrJyService;
import com.kg.module.jtnc.nzffthgl.dto.KrNzffthglDTO;
import com.kg.module.jtnc.nzffthgl.dto.convert.KrNzffthglConvert;
import com.kg.module.jtnc.nzffthgl.entity.KrNzffthgl;
import com.kg.module.jtnc.nzffthgl.service.KrNzffthglService;
import com.kg.module.jtnc.rghs.dto.KrRghsDTO;
import com.kg.module.jtnc.rghs.dto.convert.KrRghsConvert;
import com.kg.module.jtnc.rghs.entity.KrRghs;
import com.kg.module.jtnc.rghs.service.KrRghsService;
import com.kg.module.jtnc.rwgl.dto.KrRwglDTO;
import com.kg.module.jtnc.rwgl.dto.convert.KrRwglConvert;
import com.kg.module.jtnc.rwgl.entity.KrRwgl;
import com.kg.module.jtnc.rwgl.service.KrRwglService;
import com.kg.module.jtnc.util.PublicUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Sunshuwei
 * date 2024-11-05
 */
@RestController
@RequestMapping("/jtnc/applet/nh")
@Api(tags = "小程序-农户接口", value = "小程序-农户接口", description = "AppletNhController")
public class AppletNhController {
    @Resource
    private KrRwglService krRwglService;
    @Resource
    private KrRwglConvert krRwglConvert;
    @Resource
    private KrRghsService krRghsService;
    @Resource
    private KrRghsConvert krRghsConvert;
    @Resource
    private KrJyService krJyService;
    @Resource
    private KrJyConvert krJyConvert;
    @Resource
    private KrNzffthglService krNzffthglService;
    @Resource
    private KrNzffthglConvert krNzffthglConvert;

    /*****************************************************任务*****************************************************/
    @ApiOperation(notes = "/jtnc/applet/nh/listRw", value = "分页列表-任务", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "params", value = "查询条件", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping("/listRw")
    public Page<KrRwglDTO> listRw(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                  @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                                  @RequestParam(value = "params", required = false) String params) {
        JSONObject jsonObject = new JSONObject(params);
//        jsonObject.set("nhId", PublicUtil.getUserId());
        jsonObject.set("zzjhIds", PublicUtil.getZzjhIdsByNh());
        return krRwglService.pagelist(page, limit, JSONUtil.toJsonStr(jsonObject));
    }

    @ApiOperation(notes = "/jtnc/applet/nh/receiveRw", value = "领取任务", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rwId", value = "ID", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping("/receiveRw")
    public void receiveRw(String rwId) throws BaseException {
        KrRwgl byId = krRwglService.getById(rwId);
        if (!byId.getRwzt().equals("0")) {
            throw new BaseException("任务已领取或已完成");
        }
        krRwglService.lambdaUpdate()
                .set(KrRwgl::getNhId, PublicUtil.getUserId())
                .set(KrRwgl::getNhxm, PublicUtil.getUserNickName())
                .set(KrRwgl::getRwzt, 1)
                .eq(KrRwgl::getRwId, rwId)
                .update();
    }

    @ApiOperation(notes = "/jtnc/applet/nh/rwwcqr", value = "任务完成确认", httpMethod = "POST")
    @ApiImplicitParams({})
    @PostMapping("/rwwcqr")
    @NoRepeatSubmit
    public void rwwcqr(@Validated @RequestBody RwwcDTO rwwcDTO) throws BaseException {
        try {
            KrRwgl byId = krRwglService.getById(rwwcDTO.getRwId());
            if (!byId.getRwzt().equals("1")) {
                throw new BaseException("只有待完成的任务才能确认");
            }

            krRwglService.lambdaUpdate()
                    .set(KrRwgl::getRwwcsj, DateUtil.date())
                    .set(KrRwgl::getRwwcpz, rwwcDTO.getRwwcpz())
                    .set(KrRwgl::getRwwcwzms, rwwcDTO.getRwwcwzms())
                    .set(KrRwgl::getDfzt, 2)
                    .eq(KrRwgl::getRwId, rwwcDTO.getRwId())
                    .update();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "任务完成确认失败！请重试");
        }
    }


    /*****************************************************用工核算*****************************************************/

    @ApiOperation(notes = "/jtnc/applet/nh/listRghs", value = "分页列表-人工核算表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "params", value = "查询条件", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping("/listRghs")
    public Page<KrRghsDTO> listRghs(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                    @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                                    @RequestParam(value = "params", required = false) String params) {
        JSONObject jsonObject = new JSONObject(params);
        jsonObject.set("zzjhIds", PublicUtil.getZzjhIdsByNh());
        return krRghsService.pagelist(page, limit, JSONUtil.toJsonStr(jsonObject));
    }


    @ApiOperation(notes = "/jtnc/applet/nh/rghsSure", value = "人工核算确认", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rghsId", value = "ID", paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "state", value = "1:确认，2:拒绝", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping("/rghsSure")
    public void rghsSure(@RequestParam String rghsId, @RequestParam String state) throws BaseException {
        KrRghs byId = krRghsService.getById(rghsId);
        if (!byId.getState().equals("0")) {
            throw new BaseException("人工核算已确认或拒绝，不能重复操作");
        }
        krRghsService.lambdaUpdate()
                .set(KrRghs::getNhId, PublicUtil.getUserId())
                .set(KrRghs::getNhxm, PublicUtil.getUserNickName())
                .set(KrRghs::getState, state)
                .eq(KrRghs::getRghsId, rghsId)
                .update();
    }


    /*****************************************************交易****************************************************/

    @ApiOperation(notes = "/jtnc/applet/nh/listJy", value = "分页列表-收菜交易", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "params", value = "查询条件", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping("/listJy")
    public Page<KrJyDTO> listJy(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                                @RequestParam(value = "params", required = false) String params) {
        JSONObject jsonObject = new JSONObject(params);
        jsonObject.set("zzjhIds", PublicUtil.getZzjhIdsByNh());
        return krJyService.pagelist(page, limit, JSONUtil.toJsonStr(jsonObject));
    }

    @ApiOperation(notes = "/jtnc/applet/nh/jySure", value = "交易确认", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rghsId", value = "ID", paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "state", value = "1:确认，2:拒绝", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping("/jySure")
    public void jySure(@RequestParam String jyId, @RequestParam String state) throws BaseException {
        KrJy byId = krJyService.getById(jyId);
        if (!byId.getState().equals("0")) {
            throw new BaseException("交易已确认或拒绝，不能重复操作");
        }
        krJyService.lambdaUpdate()
//                .set(KrJy::getNhId, PublicUtil.getUserId())
//                .set(KrJy::getNhxm, PublicUtil.getUserNickName())
                .set(KrJy::getState, state)
                .eq(KrJy::getJyId, jyId)
                .update();
    }

    /*****************************************************农资****************************************************/

    @ApiOperation(notes = "/jtnc/applet/nh/getNzlqffById", value = "详情-农资发放或退还管理", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nzffId", value = "ID", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping("/getNzlqffById")
    public KrNzffthglDTO getById(String nzffId) {
        return krNzffthglConvert.entityToDto(krNzffthglService.getById(nzffId));
    }

    @ApiOperation(notes = "/jtnc/applet/nh/listNzlqff", value = "分页列表-农资发放或退还管理", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "params", value = "查询条件", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping("/listNzlqff")
    public Page<KrNzffthglDTO> listNzlqff(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                    @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                                    @RequestParam(value = "params", required = false) String params) {
        JSONObject jsonObject = new JSONObject(params);
        jsonObject.set("zzjhIds", PublicUtil.getZzjhIdsByNh());
        return krNzffthglService.pagelist(page, limit, JSONUtil.toJsonStr(jsonObject));
    }


    @ApiOperation(notes = "/jtnc/applet/nh/nzlqffSure", value = "农资领取发放确认", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nzffId", value = "ID", paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "state", value = "1:确认，2:拒绝", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping("/nzlqffSure")
    public void nzlqffSure(@RequestParam String nzffId, @RequestParam String state) throws BaseException {
        KrNzffthgl byId = krNzffthglService.getById(nzffId);
        if (!byId.getState().equals("0")) {
            throw new BaseException("交易已确认或拒绝，不能重复操作");
        }
        krNzffthglService.lambdaUpdate()
                .set(KrNzffthgl::getState, state)
                .eq(KrNzffthgl::getNzffId, nzffId)
                .update();
    }
}

